package com.kch.soundgame.gameplay;

import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.shape.IAreaShape;
//import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
//import org.andengine.opengl.texture.region.TiledTextureRegion;
import android.hardware.SensorManager;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.kch.soundgame.objects.PhysicsConstants;
import com.kch.soundgame.objects.GameObject;

/**
 * We
 * 
 * @author Chuan
 * 
 */
public class GamePlay extends AudioCaptureGameActivity {

	private static final int BORDER_WIDTH = 2;

	private PhysicsWorld physicsWorld;
	private Text text;

	// private TiledTextureRegion mCircleFaceTextureRegion;
	private BitmapTextureAtlas mBitmapTextureAtlas;

	private double previousAmp = 0;

	GameObject player;

	@Override
	public void onGameUpdate(float pSecondsElapsed, double amp) {
		// TODO Auto-generated method stub
		if (amp > 0) {
			text.setText(String.valueOf(amp));
			Double diff = Math.abs(previousAmp - amp);

			if ((diff > 1000 && amp > previousAmp) || amp >= 2000) {
				player.getBody().setLinearVelocity(0, -5);
			}
	

			previousAmp = amp;
		}
	}

	@Override
	public void onGameReset() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onGameCreateScene() {
		// TODO Auto-generated method stub
		text = new Text(0, 0, super.font, "Hello Android",
				this.getVertexBufferObjectManager());
		super.scene.attachChild(text);
		text.setPosition(CAMERA_WIDTH / 2 - (text.getWidth() / 2),
				CAMERA_HEIGHT / 2 - (text.getHeight() / 2));
		initPhysics();
		createBoundries();
		createPlayer(50, 50);
	}

	@Override
	public void onGameCreateResources() {
		this.mBitmapTextureAtlas = new BitmapTextureAtlas(getEngine()
				.getTextureManager(), 512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		mBitmapTextureAtlas.load();
	}

	private void initPhysics() {
		physicsWorld = new PhysicsWorld(new Vector2(0,
				SensorManager.GRAVITY_EARTH), false);
		super.scene.registerUpdateHandler(physicsWorld);
	}

	private void createPlayer(float x, float y) {
//		TiledTextureRegion mCircleFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory
//				.createTiledFromAsset(this.mBitmapTextureAtlas, this,
//						"penguin.png", 0, 128, 2, 1); // 64x32
//		AnimatedSprite playerSprite = new AnimatedSprite(x, y,
//				mCircleFaceTextureRegion, getVertexBufferObjectManager());
		
		TextureRegion mCircleFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mBitmapTextureAtlas, this,
						"ghost.png", 0, 48); // 64x32
		
		Sprite playerSprite = new Sprite(x, y, mCircleFaceTextureRegion, getVertexBufferObjectManager());

		Body playerBody = PhysicsFactory.createCircleBody(this.physicsWorld,
				playerSprite, BodyType.DynamicBody,
				PhysicsConstants.PLAYER_FIXTURE_DEF);

		player = new GameObject(playerSprite, playerBody, "Player");
		long[] frameDurration = { 100, 100 };
		player.animate(frameDurration);

		super.scene.attachChild(player.getSprite());
		this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(player
				.getSprite(), player.getBody(), true, false));

	}

	private void createBoundries() {

		IAreaShape ground = new Rectangle(0, CAMERA_HEIGHT - BORDER_WIDTH,
				CAMERA_WIDTH, BORDER_WIDTH, this.getVertexBufferObjectManager());
		IAreaShape roof = new Rectangle(0, 0, CAMERA_WIDTH, BORDER_WIDTH,
				this.getVertexBufferObjectManager());
		IAreaShape left = new Rectangle(0, 0, BORDER_WIDTH, CAMERA_HEIGHT,
				this.getVertexBufferObjectManager());
		IAreaShape right = new Rectangle(CAMERA_WIDTH - BORDER_WIDTH, 0,
				BORDER_WIDTH, CAMERA_HEIGHT,
				this.getVertexBufferObjectManager());

		PhysicsFactory.createBoxBody(this.physicsWorld, ground,
				BodyType.StaticBody, PhysicsConstants.GROUND_FIXTURE_DEF);
		PhysicsFactory.createBoxBody(this.physicsWorld, roof,
				BodyType.StaticBody, PhysicsConstants.GROUND_FIXTURE_DEF);
		PhysicsFactory.createBoxBody(this.physicsWorld, left,
				BodyType.StaticBody, PhysicsConstants.GROUND_FIXTURE_DEF);
		PhysicsFactory.createBoxBody(this.physicsWorld, right,
				BodyType.StaticBody, PhysicsConstants.GROUND_FIXTURE_DEF);

		this.scene.attachChild(ground);
		this.scene.attachChild(roof);
		this.scene.attachChild(left);
		this.scene.attachChild(right);

	}
}